package nat;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @Author: Miyako
 * @Date: 2024-07-18-17:47
 * @Description: 多线程版
 */
class  ClientHandler implements Runnable{
    private Socket socket;
    ClientHandler(Socket socket){
        this.socket = socket;
    }
    @Override
    public void run() {
        try {
        BufferedReader bw = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String str ;
        while ((str=bw.readLine()) != null) {
            System.out.println("接收信息："+str);
        }
        }catch (IOException e){
            System.out.println("Link error!");
            System.exit(-1);
        }
    }
}
public class CharServerThreadEdition {
    private ServerSocket serverSocket;
    CharServerThreadEdition(){
        try {
            System.out.println("server start...");
            serverSocket = new ServerSocket(8088);
            System.out.println("server start ok!");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("server start error!");
            System.exit(-1);
        }
    }
    public void start(){
        while (true) {
            try {
                System.out.println("Wait Client Link...");
                Socket socket = serverSocket.accept();
                System.out.println("Client Link ok!");
                new Thread(new ClientHandler(socket)).start();

            } catch (IOException e) {
                System.out.println("Link error!");
            }
        }



    }
    public static void main(String[] args) {
        new CharServerThreadEdition().start();
    }
}
